<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\HomeUserEditValidata;
use App\Http\Controllers\Api\CommonApi;
use App\Model\HomeUserModel;
use Illuminate\Support\Facades\Redis;

class HomeUserEditController extends Controller
{
    public function DoUserEdit(HomeUserEditValidata $request)
    {

        // 取出session中的用户信息
        $user = $request->session()->get('Homeuser-data');

        // 判断是否登录
        if (!$user) {
            return view('notfound');
        }

        // new图片上传类
        $api = new CommonApi;

        // 判断是否上传
        if ($request->file) {

            // 准备图片数据
            $picurl = $request->input('picurl');

            // 拿出文件后缀
            $suffix = ltrim(strrchr($picurl, '.'), '.');

            // 拿出文件名
            $test = ltrim(strrchr($picurl, '\\'), '\\');

            // 图片类型集合
            $suffixset = ['image', 'png', 'jpg', 'img','jpeg', 'gif'];

            // 判断后缀是否为图片类型
            $filesuffix = in_array($suffix, $suffixset);

            // 判断是否图片
            if (!$filesuffix) {
                return redirect()->route('user.edit')->with('message', '上传文件不合法');
            }

            // 上传图片
            $picname = $api->uploadToQiNiu($request, 'file');

            // 判断图片是否上传成功
            switch($picname['status']) {
                case '4044':
                    return redirect()->route('user.edit')->with('message', '服务器繁忙');
                break;

                case '4403':
                    return redirect()->route('user.edit')->with('message', '没有上传文件');
                break;
            }
        }

        // 获取用户信息
        $userinfo = HomeUserModel::select('phone', 'passname', 'user_status', 'created_at', 'pic')
        ->where('id', $user['id'])
        ->first();

        // 判断是否上传
        if (!isset($picname)) {
            $picname = $userinfo->pic;
        } else {
            $picname = $picname['data']['key'];
        }

        // 修改数据
        $result = HomeUserModel::where('id', $user['id'])
        ->update([
          'username' => $request->input('username'),
          'sex'      => $request->input('sex'),
          'pic'      => $picname,
        ]);

        // 准备redis键名
        $userkey = 'home_user_hash_phone:'.$userinfo->phone;

        // 查询redis是否有该用户
        $redisuser = Redis::hGetAll($userkey);

        // 如果有就更改redis用户状态
        if ($redisuser) {
            Redis::del($userkey);
            $userinfo = Redis::hMSet($userkey, [
                'id'         => $user['id'],
                'username'   => $request->input('username'),
                'passname'   => $userinfo->password,
                'sex'        => $request->input('sex'),
                'pic'        => $picname,
                'email'      => $user['email'],
                'phone'      => $userinfo->phone,
                'user_status'=> $userinfo->user_status,
                'member'     => $user['member'],
                'created_at' => $userinfo->createtime,
            ]);
        }

        // 退出登录
        session(['Homeuser-data' => []]);
        // 准备登陆数据
        $newuserinfo = [
            'id'         => $user['id'],
            'username'   => $request->input('username'),
            'sex'        => $request->input('sex'),
            'pic'        => $picname,
            'email'      => $user['email'],
            'member'     => $user['member'],
        ];

        // 登录成功
        session(['Homeuser-data' => $newuserinfo]);

        // 重新设置session

        // 返回编辑页
        return redirect()->route('user.edit');
    }
}
